package sv.mecca.action;

import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import sv.mecca.form.LoginForm;
import sv.mecca.modelo.OperacionesLogin;

public class LoginAction extends DispatchAction{
	
    public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ArrayList arreglo = new OperacionesLogin().Login((LoginForm) form, request);
        RequestDispatcher rd;
        if(arreglo.get(0).equals("error1")){
            request.setAttribute("error", "error.login0");
            rd=request.getRequestDispatcher("/Definiciones/Login.jsp");
            }
        else if (arreglo.get(0).equals("error2")){
            request.setAttribute("error", "error.login1");
            rd=request.getRequestDispatcher("/Definiciones/Login.jsp");
            }
        else if (arreglo.get(0).equals("error0")){
            request.setAttribute("error", "error.login2");
            rd=request.getRequestDispatcher("/Definiciones/Login.jsp");
            }
        else {
            request.setAttribute("exito", arreglo.get(0));
            request.setAttribute("tipo", arreglo.get(1));
            rd=request.getRequestDispatcher("/Definiciones/Inicio.jsp");
            }
        rd.forward(request, response);
        return mapping.findForward("inicio");
        }
    }